// #include "Shared.h"


Texture2D<float4> linearRT : register(t0);

SamplerState defaultSampler : register(s0);

struct VSInput
{
    float3 position : POSITION;
};

struct VSOutput
{
    float4 position : SV_POSITION;
    float2 uv : TEXCOORD;
};

float3 LinearToSRGB(float3 x)
{
    float3 low  = 12.92 * x;
    float3 high = 1.055 * pow(x, 1.0 / 2.4) - 0.055;
    return lerp(low, high, step(0.0031308, x));
}

float3 ToneMapAces(float3 color)
{
    // Cancel out the pre-exposure mentioned in
    // https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
    color *= 0.6;

    float A = 2.51;
    float B = 0.03;
    float C = 2.43;
    float D = 0.59;
    float E = 0.14;

    color = saturate((color * (A * color + B)) / (color * (C * color + D) + E));
    return color;
}

VSOutput VSMain(uint VertID: SV_VertexID)
{
    float2 ScreenUV = float2(uint2(VertID, VertID << 1) & 2);

    VSOutput output;
    output.position = float4(lerp(float2(-1, 1), float2(1, -1), ScreenUV), 1, 1);
    output.uv = ScreenUV;

    return output;
}

float4 PSMain(VSOutput input) : SV_TARGET
{
    float3 ldr = ToneMapAces(linearRT.Sample(defaultSampler, input.uv).xyz);
    return float4(LinearToSRGB(ldr), 1);
}
